<template>
    <div style="margin-top:200px;margin-left: 500px;"> <el-form ref="ruleFormRef" style="max-width: 600px"
            :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>


            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input v-model="ruleForm.pwd" show-password />
            </el-form-item>

            <el-form-item label="验证码" prop="vcode">
                <el-input v-model="ruleForm.acode" style="width: 120px; margin-right: 20px;" />
                <el-tag type="primary" @click="setVcode()">{{ ruleForm.vcode }}</el-tag>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    登录
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>

</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios';
import router from '@/router';

//let str = '123456789abcdefghijiklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ' //60
//生成四个随机字符的验证码
const setVcode = () => {
    // let str = '123456789abcdefghijiklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ' //60
    // let code = ''
    // for (var i = 0; i < 4; i++) {
    //     var random = Math.floor(Math.random() * str.length)
    //     code = code + str[random];
    // }
    // ruleForm.vcode = code;

    let A = Math.floor(Math.random() * 10);//第一个数字
    let B = Math.floor(Math.random() * 4) + 1;//1:+  2:- 3:x  4:÷ 运算符
    let C = Math.floor(Math.random() * 10);//第二个数字

    let showCode = ''//展示给用户的运算表达式例如 3x6=?
    if (B == 1) {
        ruleForm.answer = (A + C).toString();
        showCode = A + '+' + C + '=?'
    } else if (B == 2) {
        if (A < C) {
            let T = C;
            C = A;
            A = T;
        }//当A<C时,把A跟C换个位置

        ruleForm.answer = (A - C).toString();
        showCode = A + '-' + C + '=?'
    } else if (B == 3) {
        ruleForm.answer = (A * C).toString();
        showCode = A + 'x' + C + '=?'
    } else if (B == 4) {
        if (C == 0) {
            C = 1;
        }

        A = A * C;//先让A=A*C,这样AC除以C,结果永远时整数
        showCode = A + '÷' + C + '=?'
        ruleForm.answer = (A / C).toString();
    }
    ruleForm.vcode = showCode  //D.toString();


}


interface RuleForm {
    name: string
    pwd: string
    vcode: string
    acode: string
    answer: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    name: '',
    pwd: '',
    vcode: '',//标签上显示的
    acode: '', //用户自己输入的
    answer: ''//答案
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
    name: [
        { required: true, message: '请输入用户名!', trigger: 'blur' },
        { min: 1, max: 10, message: '长度', trigger: 'blur' },
    ],
    pwd: [
        { required: true, message: '请输入密码!', trigger: 'blur' },
        { min: 5, max: 10, message: '密码长度为5~10位', trigger: 'blur' },
    ],
    vcode: [
        {
            required: true,
            message: 'Please select Activity count',
            trigger: 'change',
        },
    ]
})

const submitForm = async (formEl: FormInstance | undefined) => {

    if (ruleForm.answer.toLocaleLowerCase() != ruleForm.acode.toLocaleLowerCase()) {
        ElMessage.error("验证码不正确!");
        return;
    }
    setVcode();//重置验证码

    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {//在这里写登录逻辑

            axios.get('http://localhost:5071/api/Account/Login', { params: { AccountName: ruleForm.name, AccountPass: ruleForm.pwd } })
                .then(res => {
                    console.log(res);
                    if (res.data.code == 201) {
                        ElMessage.success(res.data.msg);
                        router.push("caidan");
                    } else {
                        ElMessage.error(res.data.msg);
                    }
                });





        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))
//钩子函数 生命周期函数
onMounted(() => {
    setVcode()
});
</script>